package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.qyxx.entity.BgSjmkjgOrder;
import org.jeecg.modules.qyxx.entity.BgSwzbjcOrder;
import org.jeecg.modules.qyxx.entity.qo.SjmkjgOrderQo;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgSjmkjgOrderMapper;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description: 报告模块数据加工状态
 * @Author: ZJJ
 * @Date: 2021-06-18
 * @Version: V1.0
 */
@Service
// @DS("multi-datasource1")申报
public class BgSjmkjgOrderServiceImpl extends MppServiceImpl<BgSjmkjgOrderMapper, BgSjmkjgOrder> implements IBgSjmkjgOrderService {

    @Override
    public Page<BgSjmkjgOrder> getReportList(SjmkjgOrderQo qo) {
        Page<BgSjmkjgOrder> page = new Page();
        page.setCurrent(qo.getPageNo());
        page.setSize(qo.getPageSize());
        return baseMapper.getReportList(page,qo);
    }


    @Override
    public List<BgSjmkjgOrder> selectList(SjmkjgOrderQo qo, List<DjOrg> djOrgs) {
        if (djOrgs == null || djOrgs.size() == 0) {
            return new ArrayList<>();
        }
        List<String> shxydms = new ArrayList<>();
        // List<BgSjmkjgOrder> bgSjmkjgOrders = new ArrayList<>();
        for (DjOrg djOrg : djOrgs) {
            shxydms.add(djOrg.getShxydm());
        }
        List<BgSjmkjgOrder> bgSjmkjgOrders = baseMapper.listPage(shxydms, qo);
        for (DjOrg djOrg : djOrgs) {
            for (BgSjmkjgOrder bgSjmkjgOrder : bgSjmkjgOrders) {
                if (djOrg.getShxydm() != null && djOrg.getShxydm().equals(bgSjmkjgOrder.getDjxh())) {
                    bgSjmkjgOrder.setSaleNetCode(djOrg.getSaleNetCode());
                    bgSjmkjgOrder.setSaleNetName(djOrg.getSaleNetName());
                    bgSjmkjgOrder.setBgSsn(DateUtils.getInstance().getYearToStr(bgSjmkjgOrder.getJssj()));
                }
            }
        }
        //

        return bgSjmkjgOrders;
    }

    @Override
    public void setBgsjToRedis(BgNsrjbxxReq req) {
        DateUtils utils = DateUtils.getInstance();
        List<Date> dates = utils.findYearDates(req.getKssj(), req.getJssj());
        List<String> tjnds = new ArrayList<>();
        for (Date date : dates) {
            String year = utils.getYearToStr(date);
            if (StringUtils.hasLength(year)) {
                tjnds.add(year);
            }
        }
        tjnds = tjnds.subList(tjnds.size() - 3, tjnds.size());
        /*List<String> tjnds = new ArrayList<>();
        String scsj = utils.getYearToStr(req.getScsj());
        Integer nextOne = Integer.valueOf(scsj) - 1;
        Integer nextTwo = Integer.valueOf(scsj) - 2;
        Integer nextThree = Integer.valueOf(scsj) - 3;
        tjnds.add(nextThree.toString());
        tjnds.add(nextTwo.toString());
        tjnds.add(nextOne.toString());*/
        RedisUtil.set(RedisKeyConstant.redisBgTjndkey + req.getDjxh(), JSON.toJSONString(tjnds), 60);
        List<String> strings = JSONObject.parseArray(RedisUtil.get(RedisKeyConstant.redisBgTjndkey + req.getDjxh()).toString(), String.class);
        System.out.println(strings);
    }

    @Override
    public String getNewByDjxh(String djxh) {
        LambdaQueryWrapper<BgSjmkjgOrder> wrapper = Wrappers.lambdaQuery(BgSjmkjgOrder.class);
        wrapper.eq(BgSjmkjgOrder::getDjxh, djxh).orderByDesc(BgSjmkjgOrder::getJssj);
        Page<BgSjmkjgOrder> page = new Page<>(1,1);
        this.page(page, wrapper);
        List<BgSjmkjgOrder> list = page.getRecords();
        if (list != null && list.size() > 0) {
            BgSjmkjgOrder order = list.get(0);
            String year = org.jeecg.modules.qyxx.util.DateUtils.getInstance().getYearToStr(order.getJssj());
            return year;
        }
        return null;
    }

    @Override
    public String getNewMonthByDjxh(String djxh) {
        LambdaQueryWrapper<BgSjmkjgOrder> wrapper = Wrappers.lambdaQuery(BgSjmkjgOrder.class);
        wrapper.eq(BgSjmkjgOrder::getDjxh, djxh).orderByDesc(BgSjmkjgOrder::getJssj);
        Page<BgSjmkjgOrder> page = new Page<>(1,1);
        this.page(page, wrapper);
        List<BgSjmkjgOrder> list = page.getRecords();
        if (list != null && list.size() > 0) {
            BgSjmkjgOrder order = list.get(0);
            String month = org.jeecg.modules.qyxx.util.DateUtils.getInstance().getMonthToStr(order.getJssj());
            return month;
        }
        return null;
    }

    @Override
    public BgSjmkjgOrder getLatestByDjxh(String djxh) {
        LambdaQueryWrapper<BgSjmkjgOrder> wrapper = Wrappers.lambdaQuery(BgSjmkjgOrder.class);
        wrapper.eq(BgSjmkjgOrder::getDjxh, djxh).orderByDesc(BgSjmkjgOrder::getJssj);
        Page<BgSjmkjgOrder> page = new Page<>(1,1);
        this.page(page, wrapper);
        List<BgSjmkjgOrder> list = page.getRecords();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
